<template>
    <div id="top-bar">
        <div class="bar">
            <div class="info-bar">
                <img id="logo" src="https://ns-strategy.cdn.bcebos.com/ns-strategy/upload/fc_big_pic/part-00440-2283.jpg" alt="">    
                <div class="niugeyizhan">牛哥驿站</div>
                <div class="supplier-name">{{supplier.supplier_name}}</div>
                <i class="el-icon-phone"></i>
                <div class="wrapper">
                    <div class="collect" @click="collect">
                        <i class="el-icon-star-on" v-if="isCollect"></i> 
                        <i class="el-icon-star-off" v-else></i> 
                    </div>
                </div>
                <div class="line"></div>
                <div>
                    <span>#成交：</span>
                    <span class="num" >1.1</span>
                    <span>#评分：</span>
                    <span class="num">5</span>
                    <span>#询价：</span>
                    <span class="num">241</span>
                    <span>#访客：</span>
                    <span class="num">1789</span>
                </div>
            </div>
        </div>
        <!-- <div class="link-box">
            <div class="link-bar">
                <a @click="toSupplier('index')">
                    <span>首页</span>
                    <div class="bar"></div>
                </a>
                <a @click="toSupplier('supplyProducts')">
                    <span>产品供应</span>
                    <div class="bar"></div>
                </a>
                <a @click="toSupplier('supplierArchives')">
                    <span>企业档案</span>
                    <div class="bar"></div>
                </a>
                <a @click="toSupplier('supplierRecent')">
                    <span>企业动态</span>
                    <div class="bar"></div>
                </a>
            </div>
        </div> -->
    </div>
</template>

<script>
export default {
    name:'topBar',
    data() {
        return {
            supplier:{},

            isCollect: false,
        }
    },
    methods: {
        collect() {
            if (this.isCollect) {
                this.$API.getFavorite().then(res => {
                    if (res.status == 200) {
                        for (let obj of res.data) {
                            if (obj.supplier_id == this.$store.state.supplier.supplier_id) {
                                return this.$API.deleteFavorite(obj.id)
                            }
                        }
                    }
                }).then(res => {
                if (res.status == 200) {
                    this.isCollect = false
                    this.$message.success('收藏已取消')
                }
                })
            } else {
                this.$API.getPurchaser().then(res => {
                    if (res.status == 200) {
                        let aid = this.$store.state.user.account_id
                        for (let obj of res.data) {
                            if (obj.account_id == aid) {
                                return this.$API.addFavorite({
                                    "purchaser_id": obj.id,
                                    "product_id": null,
                                    "supplier_id": this.$store.state.supplier.supplier_id
                                })
                            }
                        }
                    }
                }).then(res => {
                if (res.status == 201) {
                    this.isCollect = true
                    this.$message.success('收藏成功')
                }
                })
            }
        },
    },
    computed: {
        sid() {
            return this.$store.state.supplier.supplier_id
        }
    },
    watch: {
        sid: {
            immediate: true,
            handler: function(newVal, oldVal) {
                if (newVal) this.$API.getOneSupplier(newVal).then(res => {
                    if (res.status == 200) {
                        this.supplier = res.data
                    }
                })
            }
        }
        
    }
}
</script>

<style lang="less" scoped>
#top-bar{
    width: 100%;
    .bar{
        background-color: @color-theme2;
        .info-bar{
            width: @width-content;
            margin: 0 auto;
            height: 100px;
            box-sizing: border-box;
            // border: 1px solid rgb(59, 168, 59);
            display: flex;
            align-items: center;
            img{
                margin-right: 10px;
            }
            #logo{
                width: 40px;
                height: 40px;
                margin-left: 50px;
                margin-top: 5px;
                text-align: center;
            }
            .niugeyizhan{
                font-size: 30px;
                color: @color-theme;
                margin-right:10px;
            }
            .supplier-name{
                font-size: 30px;
                margin-right:10px;
            }
            .wrapper{
                width: 20px;
                margin-left: 10px;
                .collect{
                    user-select: none;
                    .el-icon-star-on{
                        font-size: 1.2rem;
                        position: relative;
                        bottom: 1px;
                        color: gold;
                    }
                }
            }
            .line{
                width: 10px;
                height: 30px;
                // float: left;
                border-left: black 1px solid;
                margin: 10px 5px 10px 15px;
            }
            
        }
    }
    .link-box{
        border-top: 1px solid rgb(243, 243, 243);
        box-shadow: rgb(238 238 238) 0px 1px 0px 0px;
        position: sticky;
        top: 0;
        .link-bar{
            display: flex;
            justify-content: space-around;
            width: @width-content;
            margin: 0 auto;
            height: 50px;
            font-size: 20px;
            &:hover{
                cursor: default;
            }
            a:hover .bar{
                width: 100%;
            }
            a{
                flex: 1;
                box-sizing: border-box;
                height: 100%;
                float: left;
                margin: 0 20px;
                text-decoration: none;
                span{
                    display: inline-block;
                    width: 100%;
                    height: 100%;
                    line-height: 50px;
                    text-align: center;
                    vertical-align: middle;
                }
                .bar{
                    width: 0;
                    height: 1px;
                    background-color: rgb(71,204,71);
                    transition: width 1s;
                }
            }
        }
    }
}
</style>